#include <iostream>
#include <stdlib.h>
#include <fstream>

// dtrffwg

class sudokuLoader {
	public:

		std::string file;
		std::ifstream sudoku_file;

		std::vector<int>  rawGrid;
		std::vector<node> nodes;

		int x;
		int y;
		int c;
		int hypos;

		bool ready;

		sudokuLoader( std::string filezor ) {
			this->x = 9;
			this->y = 9;
			this->c = 0;
			this->hypos = 9;
			this->ready = false;

			this->file = filezor;
			this->prime();
		}

		void prime() {
			
			this->sudoku_file.open( this->file.c_str() );
			if (this->sudoku_file.is_open()) {
				if ( this->checkFile() ) {
					this->import();
					// std::cout << "Import Complete.\n";
					this->ready = true;
				} else {
					this->ready = false;
					// std::cout << "Failure\n";
				}
			}
			sudoku_file.close();
		}

		bool checkFile() {
			std::string line;
			std::string buf;

			std::vector<int> tokens;

			while (! this->sudoku_file.eof() ) {
				for ( int i = 0; i < tokens.size(); ++i ) {
					rawGrid.push_back(tokens[i]);
				}
				tokens.clear();

				getline( this->sudoku_file, line );
				std::stringstream ss( line );

				while ( ss >> buf ) {
					tokens.push_back(atoi(buf.c_str()));
				}

				if ( this->x != tokens.size() && this->y != this->c ) {
					return false;
				}
				this->c++;
			}
			return true;
		}
		void import() {
			for ( int i = 0; i < rawGrid.size(); ++i ) {
				node temp( this->hypos );
				if ( rawGrid[i] != 0 ) {
					temp.setStatus( rawGrid[i] - 1 );
					temp.lockNode();
				}
				nodes.push_back( temp );
			}
		}
		vector<node> getGrid() {
			return this->nodes;
		}
		bool isReady() {
			return this->ready;
		}
};
